package question;

import interceptor.InjectConnection;

import java.sql.Connection;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import utils.Var;
import utils.PageUtils;
import account.AccountUtils;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpSession;

/**
 * Created by IntelliJ IDEA.
 * User: Shawn
 * Date: 2009-6-16
 * Time: 20:55:33
 */
public class QueryAction {

  private Connection con;

  @InjectConnection
  public void setCon(Connection con) {
    this.con = con;
  }

  private List<ClassifyInfo> clist;

  public String queryTree() throws Exception {
    HttpSession session = ServletActionContext.getRequest().getSession();
    if (!AccountUtils.hasRight(session, Var.RIGHT_QUESTION)) throw new Exception("deny!");

    clist = Classify.getClassifyList(con);
    return Var.ACTION_SUCCESS;
  }

  public List<ClassifyInfo> getClist() {
    return clist;
  }

  private int cid;
  private int page;
  private long pageSum;
  private List<QuestionInfo> qlist;
  private Map<Integer, ClassifyInfo> cmap;

  public String queryQuestion() throws Exception {
    HttpSession session = ServletActionContext.getRequest().getSession();
    if (!AccountUtils.hasRight(session, Var.RIGHT_QUESTION)) throw new Exception("deny!");

    pageSum = Question.getQuestionSum(con, cid) / Var.PAGE_LENGTH + 1;
    page = PageUtils.getPage(page);
    long from = PageUtils.getFrom(page);
    qlist = Question.getQuestionList(con, cid, from, Var.PAGE_LENGTH);

    List<ClassifyInfo> list = Classify.getChildren(con, cid);
    cmap = new HashMap<Integer, ClassifyInfo>();
    for (ClassifyInfo info : list) cmap.put(info.getCid(), info);
    return Var.ACTION_SUCCESS;
  }

  public int getCid() {
    return cid;
  }

  public void setCid(int cid) {
    this.cid = cid;
  }

  public int getPage() {
    return page;
  }

  public void setPage(int page) {
    this.page = page;
  }

  public long getPageSum() {
    return pageSum;
  }

  public List<QuestionInfo> getQlist() {
    return qlist;
  }

  public Map<Integer, ClassifyInfo> getCmap() {
    return cmap;
  }
}
